package com.zzx.controller;

import com.zzx.config.MySqlChatMemory;
import com.zzx.domain.ChatHistory;
import com.zzx.service.ChatHistoryService;
import com.zzx.service.impl.ChatHistoryServiceImpl;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.function.Consumer;

@RestController
@RequestMapping("/ai")
public class ChatController {

    @Autowired
    private ChatClient chatClient;
    @Autowired
    private MySqlChatMemory mySqlChatMemory;
    @Autowired
    private ChatHistoryServiceImpl chatHistoryService;
    @GetMapping(value = "/service" ,produces = "text/html;charset=UTF-8")
    public Flux<String> serviceStream(String prompt, String chatId){
        chatHistoryService.saveChatHistory("service",chatId);
        Flux<String> content = chatClient.prompt(prompt)
                .advisors(new Consumer<ChatClient.AdvisorSpec>() {
                    @Override
                    public void accept(ChatClient.AdvisorSpec advisorSpec) {
                        advisorSpec.param(ChatMemory.CONVERSATION_ID,chatId);
                    }
                })
                .stream()
                .content();
        return content;
    }

}
